<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 分娩评估章节 -->
	<xsl:template match="*" mode="DeliveryAssessmentNoteSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_AssessmentNote]" mode="DeliveryAssessmentNoteEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 分娩评估章节条目 -->
	<xsl:template match="*" mode="DeliveryAssessmentNoteEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'分娩评估'"/>
			<xsl:with-param name="sectionType" select="'DAN'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 产后处置章节 -->
	<xsl:template match="*" mode="PostpartumHospitalizationTreatmentSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_PostpartumHospitalizationTreatment]" mode="PostpartumHospitalizationTreatmentEntry">
		</xsl:apply-templates>
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_PostpartumHospitalizationTreatment]" mode="PostpartumHospitalizationTreatment1Entry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 产后处置章节条目 (针对普通条目) -->
	<xsl:template match="*" mode="PostpartumHospitalizationTreatmentEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'产后处置'"/>
			<xsl:with-param name="sectionType" select="'PHT'"/>
		</xsl:apply-templates>
	</xsl:template>	
	<!-- 产后处置章节条目 (针对血压条目) -->
	<xsl:template match="*" mode="PostpartumHospitalizationTreatment1Entry">
		<xsl:apply-templates select="hl7:entry/hl7:organizer/hl7:component/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'产后处置'"/>
			<xsl:with-param name="sectionType" select="'PHT1'"/>
		</xsl:apply-templates>
	</xsl:template>	
	
	<!-- 产前检查章节 -->
	<xsl:template match="*" mode="PrenatalEventsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_PrenatalEvents]" mode="PrenatalEventsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 产前检查章节条目 -->
	<xsl:template match="*" mode="PrenatalEventsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'产前检查'"/>
			<xsl:with-param name="sectionType" select="'PE'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 护理标志章节 -->
	<xsl:template match="*" mode="NursingSignSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_NursingSign]" mode="NursingSignEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 护理标志章节条目 -->
	<xsl:template match="*" mode="NursingSignEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'护理标志'"/>
			<xsl:with-param name="sectionType" select="'NS'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 健康评估章节 -->
	<xsl:template match="*" mode="HealthInstructionsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_Instructions]" mode="HealthInstructionsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 健康评估章节条目 -->
	<xsl:template match="*" mode="HealthInstructionsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'健康评估'"/>
			<xsl:with-param name="sectionType" select="'HI'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 护理观察章节 -->
	<xsl:template match="*" mode="NursingObservationsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_NursingObservations]" mode="NursingObservationsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 护理观察章节条目 -->
	<xsl:template match="*" mode="NursingObservationsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'护理观察'"/>
			<xsl:with-param name="sectionType" select="'NO'"/>
		</xsl:apply-templates>
	</xsl:template>

	<!-- 皮肤章节 -->
	<xsl:template match="*" mode="IntegumentarySystemSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_IntegumentarySystem]" mode="IntegumentarySystemEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 皮肤章节条目 -->
	<xsl:template match="*" mode="IntegumentarySystemEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'皮肤'"/>
			<xsl:with-param name="sectionType" select="'IS'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 失血章节 -->
	<xsl:template match="*" mode="BloodLossSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_SurgicalOperationNoteEstimatedBloodLoss]" mode="BloodLossEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 失血章节条目 -->
	<xsl:template match="*" mode="BloodLossEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'失血'"/>
			<xsl:with-param name="sectionType" select="'BL'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 四肢章节 -->
	<xsl:template match="*" mode="ExtremitiesSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_Extremities]" mode="ExtremitiesEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 四肢章节条目 -->
	<xsl:template match="*" mode="ExtremitiesEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'四肢'"/>
			<xsl:with-param name="sectionType" select="'Ext'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 新生儿章节 -->
	<xsl:template match="*" mode="NewbornSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_NewbornDeliveryInformation]" mode="NewbornEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 新生儿章节条目 -->
	<xsl:template match="*" mode="NewbornEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'新生儿'"/>
			<xsl:with-param name="sectionType" select="'NBD'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 一般状况检查章节 -->
	<xsl:template match="*" mode="GeneralStatusSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_GeneralStatus]" mode="GeneralStatusEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 一般状况检查章节条目 -->
	<xsl:template match="*" mode="GeneralStatusEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'一般状况检查'"/>
			<xsl:with-param name="sectionType" select="'GS'"/>
		</xsl:apply-templates>
	</xsl:template>

	<!-- 月经史章节 -->
	<xsl:template match="*" mode="MenstrualHistorySection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_MenstrualHistory]" mode="MenstrualHistoryEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 月经史章节条目 -->
	<xsl:template match="*" mode="MenstrualHistoryEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'月经史'"/>
			<xsl:with-param name="sectionType" select="'MH'"/>
		</xsl:apply-templates>
	</xsl:template>

	<!-- 住院史章节 -->
	<xsl:template match="*" mode="HistoryOfHospitalizationsSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfHospitalizations]" mode="HistoryOfHospitalizationsEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 住院史章节条目 -->
	<xsl:template match="*" mode="HistoryOfHospitalizationsEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'住院史'"/>
			<xsl:with-param name="sectionType" select="'HH'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 阴道分娩章节 -->
	<xsl:template match="*" mode="LaborAndDeliveryProcessSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_LaborAndDeliveryProcess]" mode="LaborAndDeliveryProcessEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 阴道分娩章节条目 -->
	<xsl:template match="*" mode="LaborAndDeliveryProcessEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'阴道分娩'"/>
			<xsl:with-param name="sectionType" select="'LPD'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 生命体征章节 -->
	<xsl:template match="*" mode="VitalSignSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_VitalSign]" mode="VitalSignEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 生命体征章节条目 -->
	<xsl:template match="*" mode="VitalSignEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
			<xsl:with-param name="groupId" select="'生命体征'"/>
			<xsl:with-param name="sectionType" select="'VS'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 麻醉章节 -->
	<xsl:template match="*" mode="SurgicalOperationNoteAnesthesiaSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_SurgicalOperationNoteAnesthesia]" mode="SurgicalOperationNoteAnesthesiaEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 麻醉章节条目 -->
	<xsl:template match="*" mode="SurgicalOperationNoteAnesthesiaEntry">
		<!-- 仅处理《麻醉记录》的该章节 , 处理 @code='DE06.00.253.00' or @code='DE05.10.063.00' or @code='DE06.00.226.00' or @code='DE06.00.208.00' or @code='DE06.00.228.00' or @code='DE05.10.129.00'-->
		<xsl:if test="$hccDocType='C0011'">
			<xsl:apply-templates select="hl7:entry/hl7:procedure/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.253.00' or hl7:code/@code='DE05.10.063.00' or hl7:code/@code='DE06.00.226.00' or hl7:code/@code='DE06.00.208.00' or hl7:code/@code='DE06.00.228.00' or hl7:code/@code='DE05.10.129.00']" mode="HCCObservation">
				<xsl:with-param name="groupId" select="'麻醉'"/>
				<xsl:with-param name="sectionType" select="'SONA'"/>
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
	
	<!-- 体格检查章节(非《检查报告》) -->
	<xsl:template match="*" mode="PhysicalExaminationSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_PhysicalExamination]" mode="PhysicalExaminationEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 体格检查章节条目 -->
	<xsl:template match="*" mode="PhysicalExaminationEntry">
	<!-- 仅处理非《检查报告》 -->
		<xsl:if test="$hccDocType != 'C0006'">
			<xsl:apply-templates select="hl7:entry/hl7:observation" mode="HCCObservation">
				<xsl:with-param name="groupId" select="'体格检查'"/>
				<xsl:with-param name="sectionType" select="'PE'"/>
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
		
	<!--通用观察条目输出模板-->
	<xsl:template match="*" mode="HCCObservation">
		<xsl:param name = "groupId" />
		<xsl:param name = "sectionType" />
		<xsl:variable name="valueType" select="hl7:value/@xsi:type" />
		<Observation>
			<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
			<GroupId><xsl:value-of select="$groupId"/></GroupId>
			<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
			<ObservationCode>
				<Code><xsl:value-of select="hl7:code/@code"/></Code>
				<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
			</ObservationCode>
			<ObservationTime><xsl:value-of select="$encounterFromTime"/></ObservationTime>
			<ObservationValueType><xsl:value-of select="$valueType"/></ObservationValueType>
			<xsl:choose>
			    <!-- 护理观察章节Xpath路径特殊 -->
				<xsl:when test="$sectionType='NO'">
					<ObservationValue><xsl:value-of select="hl7:entryRelationship/hl7:observation/hl7:value"/></ObservationValue>
				</xsl:when>
				<xsl:when test="$valueType='CD'">
					<ObservationCodedValue>
						<Code><xsl:value-of select="hl7:value/@code"/></Code>
						<Description><xsl:value-of select="hl7:value/@displayName"/></Description>
					</ObservationCodedValue>
				</xsl:when>
				<xsl:when test="$valueType='ST'">
					<ObservationValue><xsl:value-of select="hl7:value"/></ObservationValue>
				</xsl:when>
				<xsl:otherwise>
					<ObservationValue><xsl:value-of select="hl7:value/@value"/></ObservationValue>
				</xsl:otherwise>
			</xsl:choose>
		</Observation>
	</xsl:template>
	
</xsl:stylesheet>
